package copyengine.resource.file
{
	import copyengine.resource.dto.ResInitDTO;
	import copyengine.resource.dto.ResObjectRequestParamDTO;
	import copyengine.utils.GeneralUtils;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;

	public class CEPicResourceFile extends CEBasicResFile
	{
		/**
		 * 每次Load及返回一个Loader,但在第一次load文件时候用该loader做Event.COMPLETE事件的处理
		 */
		private var picLoader:Loader;

		private var resFilePath:String;

		public function CEPicResourceFile()
		{
			super();
		}

		override protected function doInitialize(_resInitDTO:ResInitDTO):void
		{
			resFilePath = _resInitDTO.filePath;
		}

		override protected function doStartLoadFile():void
		{
			picLoader = new Loader();
			picLoader.load(new URLRequest(resFilePath), new LoaderContext(true));
			GeneralUtils.addTargetEventListener(picLoader.contentLoaderInfo, Event.COMPLETE, onLoaded);
			GeneralUtils.addTargetEventListener(picLoader.contentLoaderInfo, ProgressEvent.PROGRESS, loadFileOnProgress);
			GeneralUtils.addTargetEventListener(picLoader.contentLoaderInfo, IOErrorEvent.IO_ERROR, loadFileOnError, false, 0, true);
		}

		override protected function releaseLoader():void
		{
			if (picLoader != null)
			{
				picLoader.unloadAndStop(true);
				GeneralUtils.removeTargetEventListener(picLoader.contentLoaderInfo, Event.COMPLETE, onLoaded);
				GeneralUtils.removeTargetEventListener(picLoader.contentLoaderInfo, ProgressEvent.PROGRESS, loadFileOnProgress);
				GeneralUtils.removeTargetEventListener(picLoader.contentLoaderInfo, IOErrorEvent.IO_ERROR, loadFileOnError);
			}
			picLoader = null;
		}

		override public function getObject(_dto:ResObjectRequestParamDTO):Object
		{
			var picLoader:Loader = new Loader();
			picLoader.load(new URLRequest(resFilePath), new LoaderContext(true));
			return picLoader;
		}

		private function onLoaded(e:Event):void
		{
			loadFileComplate();
		}

	}
}
